package com.basic.modules.nykf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.basic.common.annotation.StringTrim;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.ParamsModel;
import com.basic.common.utils.R;
import com.basic.modules.nykf.entity.ConsultationRecordsEntity;
import com.basic.modules.nykf.entity.ConsultationRecordsGjEntity;
import com.basic.modules.nykf.service.ConsultationRecordsGjService;
import com.basic.modules.nykf.service.ConsultationRecordsService;
import com.basic.modules.nykf.service.PermissionService;
import com.basic.modules.sys.controller.AbstractController;
import com.basic.modules.sys.entity.SysUserEntity;
import com.basic.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;

/**
 * 咨询记录
 */
@RestController
@RequestMapping("new/ykf/consultation/records")
public class ConsultationRecordsController extends AbstractController {

    @Autowired
    PermissionService permissionService;

    @Autowired
    ConsultationRecordsService consultationRecordsService;

    @Autowired
    ConsultationRecordsGjService consultationRecordsGjService;

    @Autowired
    SysUserService sysUserService;

    /**
     * 列表-分页
     */
    @StringTrim
    @PostMapping("/getConsultationRecordsList")
    public R getConsultationRecordsList(@RequestBody ParamsModel<ConsultationRecordsEntity> params) {
        PageUtils page = consultationRecordsService.getConsultationRecordsList(params);
        return R.ok(page);
    }

    /**
     * 新增咨询记录
     *
     * @param consultationRecordsEntity
     * @return
     */
    @PostMapping("/addConsultationRecords")
    public R addConsultationRecords(@RequestBody ConsultationRecordsEntity consultationRecordsEntity) {
        return consultationRecordsService.addConsultationRecords(consultationRecordsEntity);
    }

    /**
     * 获取业务对接人集合
     *
     * @return
     */
    @PostMapping("/getYwdjrList")
    public R getYwdjrList() {
        List<SysUserEntity> sysUserEntityList = permissionService.getUserListByRoleId(14L);
        System.out.println(sysUserEntityList);
        return R.ok(sysUserEntityList);
    }

    /**
     * 客户添加业务查询客户咨询记录
     *
     * @return
     */
    @PostMapping("/getConsultationListByCustomerView")
    public R getConsultationListByCustomerView(@RequestBody ConsultationRecordsEntity consultationRecordsEntity) {
        QueryWrapper<ConsultationRecordsEntity> queryWrapper = new QueryWrapper<>();
        List<ConsultationRecordsEntity> list = consultationRecordsService.list(queryWrapper);
        return R.ok(list);
    }

    /**
     * 客户添加业务查询客户咨询记录
     *
     * @return
     */
    @PostMapping("/getConsultationInfoById")
    public R getConsultationInfoById(@RequestBody ConsultationRecordsEntity consultationRecordsEntity) {
        ConsultationRecordsEntity consultationInfo = consultationRecordsService.getById(consultationRecordsEntity.getZxid());
        Long gsdjrUserId = consultationInfo.getGsdjrUserId();
        SysUserEntity sysUser = sysUserService.getById(gsdjrUserId);
        consultationInfo.setGsdjrUserName(sysUser.getFlowerName());
        return R.ok(consultationInfo);
    }

    /**
     * 根据咨询记录ID,查询咨询记录数据
     *
     * @param consultationRecordsEntity
     * @return
     */
    @PostMapping("/getZxjlByZxId")
    public R getZxjlByZxId(@RequestBody ConsultationRecordsEntity consultationRecordsEntity) {
        ConsultationRecordsEntity consultationRecordsInfo = consultationRecordsService.getById(consultationRecordsEntity.getZxid());
        return R.ok(consultationRecordsInfo);
    }

    /**
     * 根据咨询ID获取跟进记录
     *
     * @param consultationRecordsEntity
     * @return
     */
    @PostMapping("/getGjListByZxId")
    public R getGjListByZxId(@RequestBody ConsultationRecordsEntity consultationRecordsEntity) {
        return R.ok(consultationRecordsService.getGjListByZxId(consultationRecordsEntity));
    }

    /**
     * 添加跟进记录
     *
     * @param consultationRecordsGjEntity
     * @return
     */
    @PostMapping("/addGjjl")
    public R addGjjl(@RequestBody ConsultationRecordsGjEntity consultationRecordsGjEntity) {
        if (consultationRecordsGjEntity.getGjjlId() == null) {
            return consultationRecordsGjService.saveData(consultationRecordsGjEntity);
        } else {
            return consultationRecordsGjService.updateData(consultationRecordsGjEntity);
        }
    }

    /**
     * 跟进记录-列表-分页
     */
    @StringTrim
    @PostMapping("/getConsultationGjjlList")
    public R getConsultationGjjlList(@RequestBody ParamsModel<ConsultationRecordsGjEntity> params) {
        PageUtils page = consultationRecordsGjService.getConsultationGjjlList(params);
        return R.ok(page);
    }

    /**
     * 咨询记录看板数据
     *
     * @param map
     * @return
     */
    @PostMapping("/getZxjlKbData")
    public R getZxjlKbData(@RequestBody Map map) {
        List<Map> zxjlKbData = consultationRecordsService.getZxjlKbData(map);
        return R.ok(zxjlKbData);
    }

    /**
     * 咨询记录看板柱状图
     *
     * @return
     */
    @PostMapping("/getZxjlZztData")
    public R getZxjlZztData() {
        List<Map<String, Object>> zxjlZztData = consultationRecordsService.getZxjlZztData();
        return R.ok(zxjlZztData);
    }

    /**
     * 获取日历待跟进记录数
     *
     * @param map
     * @return
     */
    @PostMapping("/getCalendarDgjjls")
    public R getCalendarDgjjls(@RequestBody Map map) {
        List<Map<String, Object>> zxjlZztData = consultationRecordsService.getCalendarDgjjls(map);
        return R.ok(zxjlZztData);
    }
}